package com.matrix.tool.ui.tool

import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.enableEdgeToEdge
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.tool.databinding.ActivityGameBinding

@Route(path = Constant.ARouterPath.GameActivityPath)
class GameActivity : BaseActivity<ActivityGameBinding>() {

    private lateinit var game: String
    private var paths: MutableList<String> = mutableListOf()
    private lateinit var currentGame: String

    override fun getViewBinding(): ActivityGameBinding {
        enableEdgeToEdge()
        return ActivityGameBinding.inflate(layoutInflater)
    }

    override fun initView() {
        game = intent.getStringExtra(Constant.ARouterString.GAME)!!
        mViewBinding.webview.settings.javaScriptEnabled = true
        mViewBinding.webview.settings.useWideViewPort = true
        mViewBinding.webview.settings.builtInZoomControls = true
        mViewBinding.webview.settings.textZoom = 100
        mViewBinding.webview.settings.loadWithOverviewMode = true;
        mViewBinding.webview.settings.useWideViewPort = true;
        mViewBinding.webview.settings.displayZoomControls = false;

        //解决跨域问题
        mViewBinding.webview.settings.allowFileAccessFromFileURLs = true
        mViewBinding.webview.settings.allowUniversalAccessFromFileURLs = true
        initPath()
    }

    private fun initPath() {
        paths.add("file:///android_asset/game_five.html")
        paths.add("file:///android_asset/tou_zi.html")
        paths.add("file:///android_asset/plane_war.html")
        paths.add("file:///android_asset/circle.html")
        paths.add("file:///android_asset/snack.html")
        paths.add("file:///android_asset/chosen_person.html")
        paths.add("file:///android_asset/shudu.html")
    }

    override fun initData() {
        when (game) {
            Constant.Value.FIVE -> currentGame = paths[0]
            Constant.Value.TOU -> currentGame = paths[1]
            Constant.Value.AIR -> currentGame = paths[2]
            Constant.Value.CIRCLE -> currentGame = paths[3]
            Constant.Value.SNACK -> currentGame = paths[4]
            Constant.Value.CHOOSE -> currentGame = paths[5]
            Constant.Value.SHUDU -> currentGame = paths[6]
        }

        mViewBinding.webview.let {
            ToastUtils.showShort("记载时间较长，请耐心等待")
            it.loadUrl(currentGame)
            it.webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView, url: String) {
                    super.onPageFinished(view, url)
                    // 页面加载完成后的操作
                }
            }
        }
    }

    override fun initListener() {
        mViewBinding.ivBack.setOnClickListener {
            finish()
        }
    }
}